/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.TMP;

import java.awt.Graphics;
import java.awt.*;
import java.awt.print.*;
import javax.swing.*;

/**
 *
 * @author Khoi
 */
public class CreatePagePrint extends JPanel implements Printable {

	public void CreatePagePrint() {

		final JFrame jf = new JFrame("Hello Frame");

		final CreatePagePrint gds = new CreatePagePrint();
		gds.setPreferredSize(new Dimension(200, 200));
		gds.setMinimumSize(new Dimension(200, 200));

		jf.getContentPane().add(gds);

		jf.setSize(400, 400);

		//invokeLater() is used as a workaround for a java
		//gui bug.
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					jf.setVisible(true);

					//get a PrintJob
					PrinterJob pjob = PrinterJob.getPrinterJob();
					//set a HelloPrint as the target to print
					pjob.setPrintable(gds, pjob.defaultPage());
					//get the print dialog, continue if canel
					//is not clicked
					if (pjob.printDialog()) {
						//print the target (HelloPrint)
						pjob.print();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * We happen to implement a paint method, but this could be ignored for
	 * components that already to something interesting on their own.
	 */
	public void paint(Graphics g) {
		super.paint(g);
		g.drawString("Hello Test!", 35, 100);
	}

	/**
	 * Printable's implementation
	 */
	public int print(Graphics g, PageFormat pf, int pageIndex) {
		//assume the page exists until proven otherwise
		int retval = Printable.PAGE_EXISTS;

		//We only want to deal with the first page.
		//The first page is numbered '0'
		if (pageIndex > 0) {
			retval = Printable.NO_SUCH_PAGE;
		} else {
			//setting up the Graphics object for printing
			g.translate((int) (pf.getImageableX()), (int) (pf.getImageableY()));
			//populate the Graphics object from HelloPrint's paint() method
			paint(g);
		}

		return retval;
	}
}
